<?php
/**
 * Created by PhpStorm.
 * User: Andy
 * Date: 2019/3/20
 * Time: 14:24
 */

namespace App\api\controller;


use Common\controller\Controller;
use Curl;
use Common\helper\FeiEPrinterHttpClient;

/**
 *
 * Class 飞鹅打印机
 * @package App\api\controller
 */
class FeiEPrinter  extends Controller
{

    private static $ip='api.feieyun.cn';
    private static $port=80;
    private  static $path='/Api/Open/';
    private  static $user='1296180534@qq.com';
    private static $ukey='gqRYPhjJRYP96AdD';
    private static $debug=1;//1-调试


    /**
     * 添加打印机
     */
    public function addPrinter(){
        $data=$this->createCommonData($apiName='Open_printerAddlist',self::$debug);
        $printerList=$_REQUEST['sn'].'#'.$_REQUEST['key'].'#'.$_REQUEST['remark'];
        $data=$data+['printerContent'=>$printerList];

        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }
    }

    /**
     * 打印
     */
    public function printData(){
        $data=$this->createCommonData($apiName='Open_printMsg',self::$debug);
        $dataPrivate=[
            'sn'=>$_REQUEST['sn'],
            'content'=>$_REQUEST['content'], //打印内容
            'times'=>isset($_REQUEST['times'])?$_REQUEST['times']:1, //打印次数，默认一次
        ];
        $data=$data+$dataPrivate;
        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }

    }

    /**
     * 删除打印机
     */
    public function delPrinter(){
        $data=$this->createCommonData($apiName='Open_printerDelList',self::$debug);
        $dataPrivate=[
            'snlist'=>$_REQUEST['snlist'],
            ];

        $data=$data+$dataPrivate;
        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }

    }

    /**
     * 修改打印机信息
     */
    public function editPrinter(){
        $data=$this->createCommonData($apiName='Open_printerEdit',self::$debug);
        $dataPrivate=[
            'sn'=>$_REQUEST['sn'],
            'name'=>$_REQUEST['name'],
            'phonenum'=>$_REQUEST['phonenum'],
        ];

        $data=$data+$dataPrivate;
        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }

    }

    /**
     * 清空打印机队列
     */
    public function clearPrinterList(){
        $data=$this->createCommonData($apiName='Open_delPrinterSqs',self::$debug);
        $dataPrivate=[
            'sn'=>$_REQUEST['sn'],
        ];

        $data=$data+$dataPrivate;
        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }

    }

    /**
     * 查询订单状态
     */
    public function checkOrderStatus(){
        $data=$this->createCommonData($apiName='Open_queryOrderState',self::$debug);
        $dataPrivate=[
            'orderid'=>$_REQUEST['orderid'],
        ];

        $data=$data+$dataPrivate;
        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }
    }


    /**
     *查看打印机 打印的总订单数
     */
    public function checkTheNumberPrintedOfAPrinterByDate(){
        $data=$this->createCommonData($apiName='Open_queryOrderInfoByDate',self::$debug);
        $dataPrivate=[
            'sn'=>$_REQUEST['sn'],
            'date'=>$_REQUEST['date'],//查询日期，格式YY-MM-DD，如：2016-09-20
        ];

        $data=$data+$dataPrivate;
        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }

    }

    /**
     * 查看打印机状态
     */
    public function checkPrinterStatus(){
        $data=$this->createCommonData($apiName='Open_queryPrinterStatus',self::$debug);
        $dataPrivate=[
            'sn'=>$_REQUEST['sn'],
        ];

        $data=$data+$dataPrivate;
        $client = new FeiEPrinterHttpClient(self::$ip,self::$port);
        if(!$client->post(self::$path,$data)){
            echo 'error';
        } else{
            echo $client->getContent();
        }

    }

    public function createSig($time){
        return sha1(self::$user.self::$ukey.$time);
    }
    /**
     * 设定全局公共参数
     * @param $apiName
     * @param $debug  debug=1则返回非json数据，测试使用
     * @return array
     */
    public function createCommonData($apiName,$debug=0){

        $time=time();
        if($debug==1){
            return [
                'user'=>self::$user,
                'stime'=>$time,
                'sig'=>sha1(self::$user.self::$ukey.$time),
                'apiname'=>$apiName,
                'debug'=>$debug
            ];

        }else{
            return [
                'user'=>self::$user,
                'stime'=>$time,
                'sig'=>sha1(self::$user.self::$ukey.$time),
                'apiname'=>$apiName,
            ];
        }


    }

}